/**
 * @file ToolMode.cpp
 *
 * @date 23/12/2011
 * @author Tales 
 *
 */

#include "ToolMode.hpp"
#include <sstream>
#include "../controller/Editor.hpp"

namespace yep
{

static const int STATE_PRESENTATION = 0, STATE_ASK = 1, STATE_FIRST_TOOL = 0x10, STATE_LAST_TOOL = 0x18, STATE_END = -1;

pair<string, ConsoleModeBase::Function> yep::ToolMode::getNextLine(Editor & editor)
{
	string text;
	Function function;
	switch (state)
	{
	case STATE_PRESENTATION:
	{
		stringstream s;
		s << "A ferramenta atualmente selecionada é: " << editor.getToolName(editor.getTool());
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_FIRST_TOOL;
		break;
	}
	case STATE_ASK:
	{
		stringstream s;
		s << "Qual ferramenta deseja selecionar? (1-" << editor.getToolCount() << " = mudar; 0 = nao mudar): ";
		function = INPUT_CHAR;
		text = s.str();
		break;
	}
	case STATE_END:
		text = "";
		function = END;
		break;
	default:
		if (state >= STATE_FIRST_TOOL && state < STATE_LAST_TOOL)
		{
			function = NO_FUNCTION;
			stringstream s;
			size_t toolIndex = state - 0x10;
			s << (toolIndex + 1) << ": " << editor.getToolName(toolIndex);
			text = s.str();
			++toolIndex;
			if (toolIndex >= editor.getToolCount())
				state = STATE_ASK;
			else
				++state;
		}
		break;
	}

	return make_pair(text, function);
}

void yep::ToolMode::handleAnswer(Editor & editor, string answer)
{
	stringstream s(answer);
	size_t toolNumber;
	s >> toolNumber;
	if (toolNumber != 0)
	{
		--toolNumber; // 0 do usuário significa "não alterar". Mas pro editor 0 significa "Tool #1", então excluímos o zero.
		if (toolNumber >= editor.getToolCount()) // Se Tool inválida? então Repita a pergunta
			return;
		editor.setTool(toolNumber);
	}
	state = STATE_END;
}

void yep::ToolMode::setup()
{
	ConsoleModeBase::setup();
	state = STATE_PRESENTATION;
}

}

/* namespace yep */
